package ptumall.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import ptumall.model.shop.Category;
import ptumall.model.shop.GoodsItem;
import ptumall.model.PageResult;
import ptumall.utils.ApiResponse;
import ptumall.service.MallService;

import java.util.List;

@RestController
@RequestMapping("/api/mall")
@RequiredArgsConstructor
public class MallController {
    private final MallService mallService;

    //查询商品分类
    @GetMapping("/categories")
    public ResponseEntity<ApiResponse<List<Category>>> getCategories() {
        List<Category> categories = mallService.getActiveCategories();
        return ResponseEntity.ok(ApiResponse.success(categories));
    }

    //查询分类下的商品
    @GetMapping("/goods")
    public ResponseEntity<ApiResponse<PageResult<GoodsItem>>> getGoodsList(
            @RequestParam Long categoryId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult<GoodsItem> result = mallService.getGoodsByCategory(categoryId, page, pageSize);
        return ResponseEntity.ok(ApiResponse.success(result));
    }

}
